﻿using System;
using System.Linq;
using System.Collections.ObjectModel;

namespace Transportr
{
    public class BrowserModel : Model
    {
        public ObservableCollection<LineItemViewModel> Items { get; private set; }

        public bool IsDataLoaded { get; private set; }

        public BrowserModel()
        {
            this.Items = new ObservableCollection<LineItemViewModel>();
        }

        public void BeginUpdate(Metadata.Filter filter)
        {
            IsDataLoaded = false;
            Service.SendRequest(ServiceModel.Function.Lines, new ServiceModel.Parameters(), EndUpdate, false);
        }

        void EndUpdate(object sender, Service.RequestState state)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    foreach (ServiceModel.Line l in state.Response.Lines.Line)
                    {
                            this.Items.Add(new LineItemViewModel()
                            {
                                lnId = l.lnId,
                                LineNumber = l.lnNumber,
                                Route = String.Format("{0} - {1}", l.endName1, l.endName2),
                                LineType = Icon.Vehicle((ServiceModel.VehicleType)l.ltId)
                            });
                    }
                    this.IsDataLoaded = true;
                });
        }
    }
}
